from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import  By
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


wd = webdriver.Chrome(service=Service(r'/Users/tonywu/downloads/Mess/chromedriver/chromedriver'))
wd.get("http://www.baidu.com")
# 隐式等待，只要是定位操作都生效，自动每隔时间会扫描下
wd.implicitly_wait(10)
try:
    # 返回第一个，没找到，抛异常
    element = wd.find_element(By.CLASS_NAME, "hotsearch-item")
    # 显示等待，条件更丰富
    element2 = WebDriverWait(wd, 10).until(
        EC.presence_of_all_elements_located((By.ID, "myElement"))
    )
    print(element.text)
    print(element.get_attribute('outerHTML'))
    print(element.get_attribute('innerHTML'))
    print(element.get_attribute('class'))
    # 返回所有，没找到，返回[]
    elements = wd.find_elements(By.CLASS_NAME, "hotsearch-item")

    for el in elements:
        print(el.text)
except NoSuchElementException as e:
    print(e.msg)

wd.quit()



